package com.cloudfun.msglisten.listener;

import cn.hutool.core.map.MapUtil;
import com.cloudfun.msglisten.common.constant.RabbitConstants;
import com.cloudfun.msglisten.common.constant.SysDictType;
import com.cloudfun.msglisten.common.model.dto.DeviceInfoDTO;
import com.cloudfun.msglisten.common.model.dto.dingtalk.TextContent;
import com.cloudfun.msglisten.service.DingTalkService;
import com.cloudfun.msglisten.service.SysDictService;
import com.cloudfun.msgsubscribe.DeviceInfo;
import com.cloudfun.msgsubscribe.RemoteMessageService;
import com.cloudfun.msgsubscribe.Topic;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;

/**
 * 设备消息发送到外部
 * Created by Huqin on 2020/6/18
 */
@Slf4j
@Component
public class DeviceMsgSendToExternalListener {

    @Autowired
    @Qualifier("externalRabbitTemplate")
    protected RabbitTemplate externalRabbitTemplate;

    @Autowired
    private SysDictService sysDictService;

    @Autowired
    private DingTalkService dingTalkService;

//    @Reference(timeout = 3_000)
//    protected RemoteMessageService remoteMessageService;

    @RabbitHandler
    @RabbitListener(queues = RabbitConstants.Queue.DEVICE_MSG_TO_EXTERNAL_MSG_LISTEN)
    public void process(DeviceInfoDTO deviceInfoDTO) {
        log.info("收到发往外部设备消息：{}", deviceInfoDTO);
        try {
            String exchangeName = deviceInfoDTO.getExchangeName();
            String appId = deviceInfoDTO.getAppId();
            DeviceInfo deviceInfo = deviceInfoDTO.getDeviceInfo();
            switch (exchangeName) {
                case RabbitConstants.Exchange.DEVICE_STATUS:
                    this.processStatus(exchangeName, appId, deviceInfo);
                    break;
                case RabbitConstants.Exchange.DEVICE_FAULT:
                    this.processFault(exchangeName, appId, deviceInfo);
                    break;
                case RabbitConstants.Exchange.DEVICE_EVENT:
                    this.processEvent(exchangeName, appId, deviceInfo);
                    break;
                default:
                    log.error("收到发往外部设备消息的exchange：{}错误", exchangeName);
            }
        } catch (Exception e) {
            log.error("收到发往外部设备消息：{}异常：{}", deviceInfoDTO, e);
            TextContent textContent = new TextContent("设备：" + deviceInfoDTO.getDeviceInfo().getImei() + "发往外部消息异常：" + e);
            dingTalkService.sendMessage(textContent);
        }
    }

    private void processStatus(String exchangeName, String appId, DeviceInfo deviceInfo) {
        if (MapUtil.isEmpty(deviceInfo.getAttrs())) {
            return;
        }
        externalRabbitTemplate.convertAndSend(exchangeName, appId, deviceInfo);
//        remoteMessageService.sendMsg(Topic.STATUS, deviceInfo);
    }

    private void processFault(String exchangeName, String appId, DeviceInfo deviceInfo) {
        if (CollectionUtils.isEmpty(deviceInfo.getAlarms())) {
            return;
        }
        externalRabbitTemplate.convertAndSend(exchangeName, appId, deviceInfo);
//        remoteMessageService.sendMsg(Topic.FAULT, deviceInfo);
    }

    private void processEvent(String exchangeName, String appId, DeviceInfo deviceInfo) {
        DeviceInfo tmp = new DeviceInfo();
        tmp.setImei(deviceInfo.getImei());
        tmp.setTimestamp(deviceInfo.getTimestamp());
        Map<String, String> availablePropMap = sysDictService.getDictByType(SysDictType.AVAILABLE_DEVICE_PROPERTY);
        Set<String> availablePropNames = availablePropMap.keySet();
        for (Map.Entry<String, String> entry : deviceInfo.getAttrs().entrySet()) {
            if (availablePropNames.contains(entry.getKey())) {
                tmp.setAttr(entry.getKey(), entry.getValue());
            }
        }
        if (MapUtil.isEmpty(tmp.getAttrs())) {
            return;
        }
        log.info("4G设备消息：{}发送到外部：{}", tmp, appId);
        externalRabbitTemplate.convertAndSend(exchangeName, appId, tmp);
//        remoteMessageService.sendMsg(Topic.EVENT, tmp);
    }

}
